package splug.cjk;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Calendar;

import splug.cjk.packet.MessagePacket;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MessageTab2 extends Activity{
	
	private EditText text_id;
	private EditText text_content;
	private String id;
	private String receiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.message_tab2);
		
		Bundle b = this.getIntent().getExtras();
		id = b.getString("ID");
		receiver = b.getString("RECEIVER");
		
		text_id = (EditText) findViewById(R.id.text_message_ID);
		text_content = (EditText) findViewById(R.id.text_message_content);
		Button btn_send = (Button) findViewById(R.id.btn_message_send_ok);
		
		text_id.setText(receiver);
		
		btn_send.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				/**
				 * 보내는 사람, 내용을 스트링으로 받아온다
				 */
				String send = text_id.getText().toString();
				String content = text_content.getText().toString();
				
				if(send.equals("")){
					Toast.makeText(MessageTab2.this, "보내실 분의 ID를 입력해주세요", Toast.LENGTH_SHORT).show();
					return;
				}
				else if(send.length() > 20){
					Toast.makeText(MessageTab2.this, "해당하는 회원을 찾을 수 없습니다", Toast.LENGTH_SHORT).show();
					return;
				}
				/**
				 * 내용이 비어있으면 예외처리
				 */
				else if(content.equals("")){
					Toast.makeText(MessageTab2.this, "내용을 입력해주세요 (최대 200자)", Toast.LENGTH_SHORT).show();
					return;
				}
				
				/**
				 * 내용이 200자가 넘어가면 예외처리
				 */
				if(content.length() > 200){
					Toast.makeText(MessageTab2.this, "200자 내로 입력해주세요", Toast.LENGTH_SHORT).show();
					return;
				}
				
				/*
				 * 현재 날짜를 스트링에 저장한다
				 */
				Calendar myCalendar = Calendar.getInstance();
				String tmp_date = Integer.toString(myCalendar
						.get(Calendar.YEAR));
				tmp_date += "/"
						+ Integer.toString(myCalendar
								.get(Calendar.MONTH) + 1);
				tmp_date += "/"
						+ Integer.toString(myCalendar
								.get(Calendar.DAY_OF_MONTH));
				tmp_date += " "
						+ Integer.toString(myCalendar
								.get(Calendar.HOUR_OF_DAY));
				tmp_date += "시 "
						+ Integer.toString(myCalendar
								.get(Calendar.MINUTE)) + "분";
				
				try {
					Socket socket = new Socket(LetsGoActivity.server_ip, 2007);

					InputStream is = socket.getInputStream();
					OutputStream os = socket.getOutputStream();

					OutputStreamWriter osw = new OutputStreamWriter(os);
					InputStreamReader isr = new InputStreamReader(is);

					osw.write(18);
					osw.flush();

					int ack = isr.read();

					if(ack == '1'){
						/**
						 * 현재 입력된 정보들을 전달해준다
						 */
						MessagePacket packet = new MessagePacket(0, id, send, null, null, content, tmp_date);
						ObjectOutputStream output = new ObjectOutputStream(os);
						output.writeObject(packet);
						
						/**
						 * ack가 '0'이면 쪽지 쓰기 실패, '1'이면 쪽지 쓰기 성공
						 */
						ack = is.read();
						if(ack == '1'){
							Toast.makeText(MessageTab2.this, "쪽지를 보냈습니다", Toast.LENGTH_SHORT).show();
							text_id.setText("");
							text_content.setText("");
						}
						else if(ack == '0'){
							Toast.makeText(MessageTab2.this, "해당하는 사용자를 찾을 수 없습니다.", Toast.LENGTH_SHORT).show();
						}
						
					}
					else{
						Toast.makeText(MessageTab2.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
						finish();
					}


				} catch (UnknownHostException e) {
					Toast.makeText(MessageTab2.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
					finish();
				} catch (IOException e) {
					Toast.makeText(MessageTab2.this, "네트워크 연결상대가 좋지 않습니다", Toast.LENGTH_SHORT).show();
					finish();
				}
			}
		});
		
	}

}
